﻿using Microsoft.EntityFrameworkCore;
using Process.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Process.Infastructure
{
    public class ProcessDbContext : DbContext
    {
        public ProcessDbContext(DbContextOptions options) : base(options)
        {

        }
        public DbSet<ProcessFlow> processes { get; set; }
        public DbSet<ProcessType> processesType { get; set; }
        public DbSet<Processinfo> processinfos { get; set; }
        public DbSet<FlowProcessRelation> flowProcessRelations { get; set; }
        // BOM 与物料
        public DbSet<BomTable> bomTables { get; set; }
        public DbSet<BomGoodsRelation> bomGoodsRelations { get; set; }
        public DbSet<BomFlowRelation> bomFlowRelations { get; set; }
        public DbSet<Goodsinfo> goodsinfos { get; set; }

        // ProcessDbContext.cs 中加入
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // 表名映射（保持与你库一致）
            modelBuilder.Entity<ProcessFlow>().ToTable("processFlows");
            modelBuilder.Entity<ProcessType>().ToTable("processTypes");
            modelBuilder.Entity<Processinfo>().ToTable("processinfo");
            modelBuilder.Entity<FlowProcessRelation>().ToTable("flowProcessRelations");
            modelBuilder.Entity<BomTable>().ToTable("bomTables");
            modelBuilder.Entity<BomGoodsRelation>().ToTable("bomGoodsRelations");
            modelBuilder.Entity<BomFlowRelation>().ToTable("bomFlowRelations");
            modelBuilder.Entity<Goodsinfo>().ToTable("goodsinfo");

            // 关键：显式指定“哪两个导航”构成一条关系，避免生成 ProcessFlowId1
            modelBuilder.Entity<FlowProcessRelation>()
                .HasOne(f => f.ProcessFlow)
                .WithMany(p => p.FlowProcessRelations)       // 指向 ProcessFlow 上的集合导航
                .HasForeignKey(f => f.ProcessFlowId)
                .IsRequired()
                .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<FlowProcessRelation>()
                .HasOne(f => f.Process)
                .WithMany()                                  // 如果 Processinfo 没有集合导航，用空集合
                .HasForeignKey(f => f.ProcessId)
                .IsRequired()
                .OnDelete(DeleteBehavior.Restrict);

            // ================= BOM 外键与删除策略 =================
            modelBuilder.Entity<BomGoodsRelation>()
                .HasOne(x => x.BomTable)
                .WithMany(b => b.BomRelations)
                .HasForeignKey(x => x.BomTableId)
                .IsRequired()
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<BomGoodsRelation>()
                .HasOne(x => x.Goodsinfo)
                .WithMany()
                .HasForeignKey(x => x.GoodsinfoId)
                .IsRequired()
                .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<BomFlowRelation>()
                .HasOne(x => x.BomTable)
                .WithMany(b => b.BomFlowRelation)
                .HasForeignKey(x => x.BomTableId)
                .IsRequired()
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<BomFlowRelation>()
                .HasOne(x => x.ProcessFlow)
                .WithMany()
                .HasForeignKey(x => x.ProcessFlowId)
                .IsRequired()
                .OnDelete(DeleteBehavior.Restrict);

            // BOMCode + Version 唯一索引（可选）
            modelBuilder.Entity<BomTable>()
                .HasIndex(x => new { x.BOMCode, x.Version })
                .IsUnique();
        }
    }
}
